package com.mantru.iotpot.Cook;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mantru.iotpot.ApplicationClass;
import com.mantru.iotpot.Cook.adapter.StepDiagramRecyclerViewAdapter;
import com.mantru.iotpot.DateClass.StepDiagramDate;
import com.mantru.iotpot.R;
import com.mantru.iotpot.Tools.ImageLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class StepDiagramActivity extends AppCompatActivity implements View.OnClickListener {
    private ApplicationClass ac;
    private LinearLayout back1;
    private LinearLayout back2;
    private TextView FoodName;
    private ImageView foodImg;
    private TextView textView1;
    private TextView time;
    private TextView foodnum;
    private String num;
    private String endTime;
    private String foodname;
    private RecyclerView recyclerView;
    private String img;
    private List<StepDiagramDate> stepList;
    private StepDiagramRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_step_diagram);
        ac = (ApplicationClass) getApplication();// 初始化 ApplicationClass
        // 读取保存的值
        SharedPreferences sharedPreferences = getSharedPreferences("CookPreferences", MODE_PRIVATE);
        num = sharedPreferences.getString("food_num", "");
        endTime = sharedPreferences.getString("end_time", "");
        foodname = sharedPreferences.getString("food_name","");
        img = sharedPreferences.getString("food_img","");
        Log.d("StepDiagramActivity", "读取的 num: " + num + ", end_time: " + endTime
                + ", food_name=" + foodname + ", food_img=" + img);
        initialization();//初始化控件
        analysis();//解析json数据并且将数据加载到适配器
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        // 将解析后的数据传递给适配器
        Log.d("StepDiagramActivity", "开始加载适配器\n" + stepList);
        Log.d("StepDiagramActivity", "stepList.size(): " + stepList.size());
        adapter = new StepDiagramRecyclerViewAdapter(stepList);
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    //控件初始化
    public void initialization(){
        back1 = findViewById(R.id.step_diagram_back);
        back2 = findViewById(R.id.step_diagram_step);
        FoodName = findViewById(R.id.step_diagram_name);
        foodImg = findViewById(R.id.step_diagramfood_img);
        textView1 = findViewById(R.id.step_diagramfood_text1);
        time = findViewById(R.id.step_diagramfood_time);
        foodnum = findViewById(R.id.step_diagram_num);
        recyclerView = findViewById(R.id.step_diagram_recyclerView);
        back1.setOnClickListener(this);
        back2.setOnClickListener(this);
        FoodName.setText(foodname);
        time.setText("持续时间：" + endTime);
        textView1.setText(foodname);
        foodnum.setText(num + "份");
        if (img != null){
            ImageLoader.loadImage(img, foodImg);// 使用 ImageLoader 加载图片
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.step_diagram_back){//点击返回
            finish();
        } else if (v.getId() == R.id.step_diagram_step) {
            finish();
        }
    }

    //解析json数据
    public void analysis() {
        String result = ac.jobmaster.getJsonString_Steep();
        Log.d("StepDiagramActivity", "result: " + result);
        stepList = new ArrayList<>();
        try {
            // 解析最外层的 JSON 对象
            JSONObject rootObject = new JSONObject(result);
            // 获取 data 数组
            JSONArray dataArray = rootObject.getJSONArray("data");
            // 遍历 data 数组
            for (int i = 0; i < dataArray.length(); i++) {
                JSONObject stepObject = dataArray.getJSONObject(i);
                // 提取字段
                int steepIndex = stepObject.getInt("steep_index");
                String steepName = stepObject.getString("steep_name");
                int steepStatus = stepObject.getInt("steep_status");
                int remainTime = stepObject.getInt("remain_time");
                int totalTime = stepObject.getInt("total_time");
                // 提取 data 数组
                JSONArray dataJSONArray = stepObject.getJSONArray("data");
                List<String> data = new ArrayList<>();
                for (int j = 0; j < dataJSONArray.length(); j++) {
                    data.add(dataJSONArray.getString(j));
                }
                // 创建 StepDiagramDate 对象并添加到列表
                StepDiagramDate step = new StepDiagramDate(
                        steepIndex,
                        steepName,
                        steepStatus,
                        remainTime,
                        totalTime,
                        data
                );
                stepList.add(step);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Log.d("StepDiagramActivity", "JSON 解析错误: " + e.getMessage());
        }
    }
}